package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.VideoDao;
import com.hp.entity.Link;
import com.hp.entity.Video;
import com.hp.process.KeepAlive;
import com.hp.process.Server;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.VideoService;
import com.hp.utils.UUIDBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Transactional
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private Server server;

    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo page(int page, int rows, Video video) throws Exception {
        Page<Link> startPage = PageHelper.startPage(page, rows);//使用分页插件方法
        videoDao.findAll(video == null ? new Video() : video);//插件方法必须在查询数据之前
        //封装数据
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());//当前查询的数据
        dataGridResultInfo.setTotal((int) startPage.getTotal());//总页数
        return dataGridResultInfo;
    }

    @Override
    public SubmitResultInfo addsubmit(CommonsMultipartFile file, Video video, HttpServletRequest request) throws Exception {

        if (video == null) {
            ResultUtil.createFail(Config.MESSAGE, 211,
                    null);
        }
        // 先处理上传的视频
        if (file != null && !file.isEmpty()) {
            String realPath = request.getSession().getServletContext()
                    .getRealPath("/upload/video/");
            //判断文件夹是否存在，没有就创建
            File fff = new File(realPath);
            if (!fff.exists()) {
                fff.mkdir();
            }

            File f = new File(realPath + "/" + file.getOriginalFilename());
            file.transferTo(f);


            video.setVideo(Config.BaseUrl + "upload/video/" + file.getOriginalFilename());
        }
        // 保存资源
        video.setId(UUIDBuild.getUUID());
        video.setName(file.getOriginalFilename());
        videoDao.add(video);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 661, new Object[]{"视频"}));
    }

    @Transactional(readOnly = true)
    @Override
    public Video findById(String id) throws Exception {
        return videoDao.findById(id);
    }

    @Override
    public SubmitResultInfo updatesubmit(CommonsMultipartFile file, Video video, HttpServletRequest request) throws Exception {
        if (video == null) {
            ResultUtil.createFail(Config.MESSAGE, 211,
                    null);
        }
        // 先处理上传的视频
        if (file != null && !file.isEmpty()) {
            String realPath = request.getSession().getServletContext()
                    .getRealPath("//upload//video//");


            //删除旧的视频
            String oldvideo = this.findById(video.getId()).getVideo().replace(Config.BaseUrl + "upload//", "");
            //判断是否存在
            File fff = new File(realPath + "//" + oldvideo);
            if (fff.isFile()&&fff.exists()) {
                fff.delete();
            }
            //判断是否有保存路径
            File d=new File(realPath);
            if (!d.exists()){
                d.mkdirs();
            }


            //复制
            File f = new File(realPath + "//" + file.getOriginalFilename());
            file.transferTo(f);
            video.setVideo(Config.BaseUrl + "upload//video//" + file.getOriginalFilename());
            video.setName(file.getOriginalFilename());
        }
        // 修改资源
        videoDao.update(video);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 666, new Object[]{"视频"}));
    }

    @Override
    public SubmitResultInfo delete(String id, HttpServletRequest request) throws Exception {
        String realPath = request.getSession().getServletContext()
                .getRealPath("//upload//video//");
        Video video = this.findById(id);
        //删除旧的视频
        String oldvideo = video.getVideo().replace(Config.BaseUrl + "upload//video//", "");
        //判断是否存在
        File fff = new File(realPath + "//" + oldvideo);
        if (fff.isFile()) {
            fff.delete();
        }

        videoDao.delete(id);


        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 662, new Object[]{"视频"}));
    }

    @Transactional(readOnly = true)
    @Override
    public SubmitResultInfo sendSubmit(String kid, String id) throws Exception {
        Video video = videoDao.findById(id);
        KeepAlive keepAlive = server.getDatalist().get(kid);
        keepAlive.setFlag(true);//有任务
        keepAlive.setStatus(1);//表示下载
        keepAlive.setMsg(video.getVideo());
        server.getDatalist().put(kid,keepAlive);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 668, null));
    }
}
